﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Digitalmes.Model.EquipmentManagement;
using Digitalmes.Model;

namespace Digitalmes.ViewModel.EquipmentManagement.MaintenanceRecordVMs
{
    public partial class MaintenanceRecordListVM : BasePagedListVM<MaintenanceRecord_View, MaintenanceRecordSearcher>
    {
        
        protected override IEnumerable<IGridColumn<MaintenanceRecord_View>> InitGridHeader()
        {
            return new List<GridColumn<MaintenanceRecord_View>>{
                
                this.MakeGridHeader(x => x.MaintenanceRecord_Plan).SetTitle(@Localizer["Page.关联计划"].Value),
                this.MakeGridHeader(x => x.MaintenanceRecord_Equipment).SetTitle(@Localizer["Page.关联设备"].Value),
                this.MakeGridHeader(x => x.MaintenanceRecord_Maintainer).SetTitle(@Localizer["Page.维护人员"].Value),
                this.MakeGridHeader(x => x.MaintenanceRecord_MaintenanceTime).SetTitle(@Localizer["Page.维护时间"].Value),
                this.MakeGridHeader(x => x.MaintenanceRecord_FinishTime).SetTitle(@Localizer["Page.完成时间"].Value),
                this.MakeGridHeader(x => x.MaintenanceRecord_ActualHours).SetTitle(@Localizer["Page.实际工时"].Value),
                this.MakeGridHeader(x => x.MaintenanceRecord_Result).SetTitle(@Localizer["Page.维护结果"].Value),
                this.MakeGridHeader(x => x.MaintenanceRecord_Note).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.MaintenanceRecord_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.MaintenanceRecord_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.MaintenanceRecord_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.MaintenanceRecord_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),

                this.MakeGridHeaderAction(width: 200)
            };
        }

        
        public override IOrderedQueryable<MaintenanceRecord_View> GetSearchQuery()
        {
            var query = DC.Set<MaintenanceRecord>()
                
                .CheckEqual(Searcher.PlanId, x=>x.PlanId)
                .CheckEqual(Searcher.EquipmentId, x=>x.EquipmentId)
                .CheckEqual(Searcher.MaintainerId, x=>x.MaintainerId)
                .CheckBetween(Searcher.MaintenanceTime?.GetStartTime(), Searcher.MaintenanceTime?.GetEndTime(), x => x.MaintenanceTime, includeMax: false)
                .CheckBetween(Searcher.FinishTime?.GetStartTime(), Searcher.FinishTime?.GetEndTime(), x => x.FinishTime, includeMax: false)
                .CheckEqual(Searcher.ActualHours, x=>x.ActualHours)
                .CheckEqual(Searcher.Result, x=>x.Result)
                .CheckContain(Searcher.Note, x=>x.Note)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x=>x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x=>x.UpdateBy)
                .Select(x => new MaintenanceRecord_View
                {
				    ID = x.ID,
                    
                    MaintenanceRecord_Plan = x.Plan.TenantCode,
                    MaintenanceRecord_Equipment = x.Equipment.ID,
                    MaintenanceRecord_Maintainer = DC.Set<FrameworkUser>().Where(z0 => z0.ITCode == x.MaintainerId).Select(y => y.Name).FirstOrDefault(),
                    MaintenanceRecord_MaintenanceTime = x.MaintenanceTime,
                    MaintenanceRecord_FinishTime = x.FinishTime,
                    MaintenanceRecord_ActualHours = x.ActualHours,
                    MaintenanceRecord_Result = x.Result,
                    MaintenanceRecord_Note = x.Note,
                    MaintenanceRecord_CreateTime = x.CreateTime,
                    MaintenanceRecord_UpdateTime = x.UpdateTime,
                    MaintenanceRecord_CreateBy = x.CreateBy,
                    MaintenanceRecord_UpdateBy = x.UpdateBy,
                })
                .OrderBy(x => x.ID);
            return query;
        }

        
    }
    public class MaintenanceRecord_View: MaintenanceRecord
    {
        
        public string MaintenanceRecord_Plan { get; set; }
        public long? MaintenanceRecord_Equipment { get; set; }
        public string MaintenanceRecord_Maintainer { get; set; }
        public DateTime? MaintenanceRecord_MaintenanceTime { get; set; }
        public DateTime? MaintenanceRecord_FinishTime { get; set; }
        public decimal? MaintenanceRecord_ActualHours { get; set; }
        public MaintenanceResultEnum? MaintenanceRecord_Result { get; set; }
        public string MaintenanceRecord_Note { get; set; }
        public DateTime? MaintenanceRecord_CreateTime { get; set; }
        public DateTime? MaintenanceRecord_UpdateTime { get; set; }
        public string MaintenanceRecord_CreateBy { get; set; }
        public string MaintenanceRecord_UpdateBy { get; set; }

    }

}